package org.example.util;

import com.baomidou.mybatisplus.core.MybatisSqlSessionFactoryBuilder;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.ly.cinema.mapper.MovieMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Paths;

public class MyBatisUtils {

    public static void main(String[] args) {
        // generator();
//        try (SqlSession session = getSession();){
//            MovieMapper mapper = session.getMapper(MovieMapper.class);
//            System.out.println("mapper.selectById(1) = " + mapper.selectById(1));
//        }
        generator("jdbc:mysql:///damai", "root","a", "com.yc.damai");
    }

    public static void generator(String url, String username, String password, String packageName) {
        FastAutoGenerator.create(url, username, password)
                .globalConfig(builder -> builder
                        .author("LiaoYan")
                        .outputDir(Paths.get(System.getProperty("user.dir")) + "/src/main/java")
                        .commentDate("yyyy-MM-dd")
                )
                .packageConfig(builder -> builder
                        .parent(packageName)
                        .entity("entity")
                        .mapper("mapper")
                        .service("service")
                        .serviceImpl("service.impl")
                        .xml("mapper.xml")
                )
                .strategyConfig(builder -> builder
                        .entityBuilder()
                        .enableLombok()
                )
                // 模板引擎, 默认为 velocity
//                .templateEngine(new FreemarkerTemplateEngine())
                .execute();

    }

    private static SqlSessionFactory sqlSessionFactory;
    static {
        try{
            String resource = "mybatis.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            // 构建会话工厂
            MybatisSqlSessionFactoryBuilder builder = new MybatisSqlSessionFactoryBuilder();
            sqlSessionFactory = builder.build(inputStream);
            // 获取 Mybatis 配置对象, 进行扩展配置 ( 分页插件 )
            Configuration configuration = sqlSessionFactory.getConfiguration();
            // 创建 MyBatisPlus 插件对象
            MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
            // 构建 MyBatisPlus 分页插件
            PaginationInnerInterceptor pageInterceptor = new PaginationInnerInterceptor();
            // 设置分页插件
            interceptor.addInnerInterceptor(pageInterceptor);
            // 添加 MyBatisPlus 拦截器插件
            configuration.addInterceptor(interceptor);
        } catch (IOException e){
            throw new RuntimeException(e);
        }
    }

    public static SqlSession getSession() {
        return sqlSessionFactory.openSession(true);
    }

    public static SqlSession getSession(boolean autoCommit) {
        return sqlSessionFactory.openSession(autoCommit);
    }



}
